package org.bud.boot.result;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author shaun
 * @date 2024/7/19
 * @instruction
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class R<T> {
    private boolean success;

    private int statusCode;

    private T data;

    private String msg;

    public static <T> R<T> ok(T data){
        return r(data,StatusCode.OK);
    }

    public static <T> R<T> fail(){
        return r(null,StatusCode.FAIL);
    }

    public static <T> R<T> r(T data,StatusCode statusCode){
        return R.<T>builder().success(statusCode == StatusCode.OK)
                .statusCode(statusCode.code)
                .data(data)
                .msg(statusCode.desc)
                .build();
    }

    public static <T> R<T> fail(T data,StatusCode statusCode){
        return R.<T>builder().success(false)
                .statusCode(statusCode.code)
                .data(data)
                .msg(statusCode.desc)
                .build();
    }
}
